using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HPUIMgr : MonoBehaviour
{
    public Sprite[] _HPSt;
    private Image[] _HPImg;
    private RectTransform[] _HPTf;
    private Canvas _canvas;
    private PropModule _PM;

    public float _HPScaleRatio;
    public float _voidHPScaleRatio;
    public float _dmgHPScaleRatio;

    private void Start() { }

    private void Update() { }

    public void IniDspl(PropModule UIPM)
    {
        _HPImg = GetComponentsInChildren<Image>();
        _HPTf = gameObject.GetComponentsInChildren<RectTransform>();
        _canvas = GetComponent<Canvas>();
        _PM = UIPM;
        for (int i = 0; i < _HPTf.Length - 1; i++) _HPTf[i] = _HPTf[i + 1];//del the first element, the self component
    }

    public void Dspl() { _canvas.enabled = true; }
    public void Close() { _canvas.enabled = false; }

    public void Rfr()
    {
        for (int i = 0; i < _PM.CurAvlHP; i++) RealPoint(i);
        for (int i = _PM.CurAvlHP; i < _PM.MaxAvlHP; i++) VoidPoint(i);
        for (int i = _PM.MaxAvlHP; i < _PM.CurMaxHP; i++) DmgPoint(i);
        for (int i = _PM.CurMaxHP; i < _PM.MaxHP; i++) _HPImg[i].enabled = false;
    }

    /// <summary>
    /// Set point real.
    /// </summary>
    /// <param name="idx"></param>
    private void RealPoint(int idx)
    {
        _HPImg[idx].enabled = true;
        _HPImg[idx].sprite = _HPSt[0];//real HP
        _HPTf[idx].localScale = Vector3.one * _HPScaleRatio;//as big as the real HP
    }

    /// <summary>
    /// Set point void st.
    /// </summary>
    /// <param name="idx"></param>
    private void VoidPoint(int idx)
    {
        _HPImg[idx].enabled = true;
        _HPImg[idx].sprite = _HPSt[0];//real HP
        _HPTf[idx].localScale = Vector3.one * _voidHPScaleRatio;
    }

    /// <summary>
    /// Set point dmg st.
    /// </summary>
    /// <param name="idx"></param>
    private void DmgPoint(int idx)
    {
        _HPImg[idx].enabled = true;
        _HPImg[idx].sprite = _HPSt[1];//damaged HP
        _HPTf[idx].localScale = Vector3.one * _dmgHPScaleRatio;
    }
}
